<template>
     <el-form :model="tableForm" :rules="tableRule" ref="tableForm" label-width="100px">
       <el-form-item label="卡号" prop="account">
           <el-input size="small" v-model="tableForm.account" auto-complete="off" placeholder="请输入卡号"></el-input>
       </el-form-item>

       <el-form-item label="姓名" prop="name">
           <el-input size="small" v-model="tableForm.name" auto-complete="off" placeholder="请输入姓名"></el-input>
       </el-form-item>

       <el-form-item label="性别" prop="sex">
         <el-radio-group v-model="tableForm.sex">
           <el-radio :label="0">男</el-radio>
           <el-radio :label="1">女</el-radio>
         </el-radio-group>
       </el-form-item>

       <el-form-item label="手机号" prop="phone">
         <el-input size="small" v-model="tableForm.phone" auto-complete="off" placeholder="请输入手机号"></el-input>
       </el-form-item>

       <el-form-item label="身份证" prop="card">
         <el-input size="small" v-model="tableForm.card" auto-complete="off" placeholder="请输入身份证"></el-input>
       </el-form-item>

       <el-form-item label="家庭住址" prop="address">
         <el-input size="small" v-model="tableForm.address" auto-complete="off" placeholder="请输入家庭住址"></el-input>
       </el-form-item>

       <el-form-item label="病史" prop="medical">
         <el-input size="small" v-model="tableForm.medical" auto-complete="off" placeholder="请输入病史"></el-input>
       </el-form-item>

       <el-form-item label="过敏史" prop="allergy">
         <el-input size="small" v-model="tableForm.allergy" auto-complete="off" placeholder="请输入过敏史"></el-input>
       </el-form-item>

       <el-form-item label="药物使用史" prop="drug">
         <el-input size="small" v-model="tableForm.drug" auto-complete="off" placeholder="请输入药物使用史"></el-input>
       </el-form-item>

     </el-form>
</template>

<script>

import {addData, updateData} from '@/api/user'

// 表单校验
const tableRule = {
  account: [{ required: true, message: '请输入卡号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
};

export default {
  name: 'UserEdit',
  props: {
    formData :{
      type: Object
    }
  },
  data() {
    return {
      tableForm: this.formData,
      tableRule: tableRule
    }
  },
  methods:{
    //表单保存事件
    submitForm() {
      this.$refs["tableForm"].validate(valid => {
        if (valid) {
          if(this.tableForm.id == ""){
            addData(this.tableForm).then(res => {
              if (res.message == "success" ) {
                this.$message({
                  type: 'success',
                  message: '新增成功！'
                });
                this.$emit("closeDialog", false);
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                });
              }
            });
          }else{
            updateData(this.tableForm).then(res => {
              if (res.message == "success" ) {
                this.$message({
                  type: 'success',
                  message: '修改成功！'
                });
                this.$emit("closeDialog", false);
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    clearForm(){
      this.$refs.tableForm.resetFields();
      this.$refs.tableForm.clearValidate();
    }
  }
}
</script>

<style scoped>


</style>
